package com.chancingpack.controller;

import com.alibaba.fastjson.JSONObject;
import com.chancingpack.bean.WxSendMessageBean;
import com.chancingpack.dal.model.WxReceiveMessage;
import com.chancingpack.dal.model.WxSendMessage;
import com.chancingpack.service.WxReceiveMessageService;
import com.chancingpack.service.WxSendMessageService;
import com.chancingpack.service.WxUserLoginService;
import com.chancingpack.util.CommonPage;
import com.chancingpack.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/7/14.
 */
@Controller
@RequestMapping("/send")
public class WxSendMessageController {

    @Autowired
    private WxSendMessageService wxSendMessageService;
    @Autowired
    private WxReceiveMessageService wxReceiveMessageService;
/**
 * 收发货页面
* */
    @RequestMapping("/sendindex")
    public ModelAndView index(){
        ModelAndView view = new ModelAndView("logistics/addSendMessage");
        view.addObject("CustNameList",wxSendMessageService.selectCustName());
        return view;
    }
/**
 * 通过客户id查询收发货信息
* */
    @RequestMapping("/saveSend")
    @ResponseBody
    public CommonResult saveSend(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = this.wxSendMessageService.selectSendpage(commonPage,request,response);
        if (result.getIsSuccess()) {
            result.setData(commonPage);
        }
        return result;
    }
/**
 * 添加发货信息页面
* */
    @RequestMapping("/insertSend")
    public ModelAndView insertSend(String compaCode,Integer detailId  ){
        ModelAndView view = new ModelAndView("logistics/addSend");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("compaCode",compaCode);
        map.put("detailId",detailId);
        List<Map<String,Object>> sendAll = wxSendMessageService.selectAll(map);
        view.addObject("sendAll",sendAll);
        return view;
    }
    /**
     * 添加发货信息
    * */
    @RequestMapping(value = "/addSend",produces="application/json;charset=UTF-8")
    @ResponseBody
    public CommonResult addSend(@RequestBody WxSendMessage WxSendMessage) {
        CommonResult result = this.wxSendMessageService.addtsend(WxSendMessage);
        return result;
    }
    /**
     * 添加收货信息页面
     * */
    @RequestMapping("/insertReceive")
    public ModelAndView insertReceive(String compaCode,Integer detailId  ){
        ModelAndView view = new ModelAndView("logistics/addReceive");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("compaCode",compaCode);
        map.put("detailId",detailId);
        List<Map<String,Object>> sendAll = wxSendMessageService.selectAll(map);
        view.addObject("sendAll",sendAll);
        return view;
    }
    /**
     * 添加收货信息
     * */
    @RequestMapping(value = "/addReceive",produces="application/json;charset=UTF-8")
    @ResponseBody
    public CommonResult addReceive(@RequestBody WxReceiveMessage record) {
        CommonResult result = this.wxReceiveMessageService.insertReceive(record);
        return result;
    }
/**
 * 查看收发货信息
 * */
    @RequestMapping("/selectSendReceiveListData")
    public ModelAndView selectSendReceiveListData(String compaCode,Integer detailId){
        ModelAndView view = new ModelAndView("logistics/selectSendReceiveListData");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("compaCode",compaCode);
        map.put("detailId",detailId);
        List<Map<String,Object>> sendAll = wxSendMessageService.selectSendBydetailId(map);
        List<Map<String,Object>> receiveAll = wxReceiveMessageService.selectReceiveBydetailId(map);
        if(sendAll.size()==0){
            view.addObject("secount",false);
        }else{
            view.addObject("sendAll",sendAll);
            view.addObject("secount",true);
        }
        if (receiveAll.size()==0){
            view.addObject("recount",false);
        }else{
            view.addObject("receiveAll",receiveAll);
            view.addObject("recount",true);
        }

        return view;
    }
}
